define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/task/lists/index',
                    add_url: 'shop/task/lists/add',
                    edit_url: 'shop/task/lists/edit',
                    del_url: 'shop/task/lists/del',
                    multi_url: 'shop/task/lists/multi',
                    table: 'shop_task',
                    div_evn:{
                        btn_1:{
                            divfunction:function(row){
                                var data = table.bootstrapTable('getSelections');
                                var table_select = JSON.stringify(table.bootstrapTable('getSelections'));
                                layer.confirm('确定发布？',{
                                    btn: ['确定','取消'] //可以无限个按钮
                                    },function(index, layero){
                                        Fast.api.ajax({
                                            url: 'shop/task/lists/update_status',
                                            data: {
                                             'row[ids]': row['task_id'],
                                            },
                                            success: function (data) {
                                               table.bootstrapTable('refresh');
                                               layer.msg(data.msg);
                                            },
                                            error: function (data) {
                                                table.bootstrapTable('refresh');
                                               //失败的回调
                                              layer.msg(data.msg);
                                            }
                                        });
                                    },function(index, layero){
                                        layer.msg('已取消操作。');
                                    }
                                );
                            }
                        }
                    },
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'task_id',
                sortName: 'task_id',
                columns: [
                    [
                        {checkbox: true},
                        //{field: 'task_id', title: __('Task_id')},
                        {field: 'task_sno', title: __('Task_sno'),operate:false},
                        //{field: 'platform_type', title: __('Platform_type')},
                        {field: 'shop_name', title: __('Shop_name')},
                        // {field: 'shop_admin_id', title: __('Shop_admin_id')},
                        // {field: 'goods_main_pic', title: __('Goods_main_pic')},
                        // {field: 'pay_type', title: __('Pay_type')},
                        // {field: 'goods_url', title: __('Goods_url'), formatter: Table.api.formatter.url},
                        {field: 'goods_sno', title: __('Goods_sno'),operate:false},
                        {field: 'key_word', title: __('key_word')},
                        // {field: 'beat_price', title: __('Beat_price'), operate:'BETWEEN'},
                        {field: 'beat_price', title: __('Service_change_price'), operate:'BETWEEN',operate:false},
                        // {field: 'ranking_type', title: __('Ranking_type')},
                        // {field: 'ranking', title: __('Ranking')},
                        // {field: 'price_limit', title: __('Price_limit')},
                        // {field: 'task_type', title: __('Task_type')},
                        // {field: 'browse_time', title: __('Browse_time')},
                        // {field: 'is_good_evalute', title: __('Is_good_evalute')},
                        // {field: 'is_review', title: __('Is_review')},
                        // {field: 'is_review_slide_show', title: __('Is_review_slide_show')},
                        // {field: 'is_need_send', title: __('Is_need_send')},
                        // {field: 'is_collect', title: __('Is_collect')},
                        // {field: 'is_fake_check', title: __('Is_fake_check')},
                        // {field: 'other_requirement', title: __('Other_requirement')},
                        // {field: 'total_task_num', title: __('Total_task_num')},
                        // {field: 'left_task_num', title: __('Left_task_num')},

                        {field: 'task_status', title: __('Task_status')},
                        //     ,searchList: {
                        //     "1":__('未发布'),
                        //     "2":__('已发布'),
                        // },formatter:function(value,row,index){
                        //         var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
                        //         var index = keys.indexOf(value.toString());
                        //         value = value === null ? '' : value.toString();
                        //         var display = index > -1 ? this.searchList[value] : null;
                        //         var icon = typeof this.icon !== 'undefined' ? this.icon : null;
                        //         if (!display) {
                        //             display = __(value.charAt(0).toUpperCase() + value.slice(1));
                        //         }
                        //         var html = '<span class="text">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';
                        //
                        //         return html;
                        //     }},
                        {field: 't.create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'delete_time', title: __('Delete_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table,

                            events: Table.api.events.operate, formatter: Table.api.formatter.operate,formatter: function (value, row, index) {
                                var table = this.table;
                                // 操作配置
                                var options = table ? table.bootstrapTable('getOptions') : {};
                                // 默认按钮组
                                var buttons = $.extend([], this.buttons || []);
                                if (options.extend.del_url !== '') {
                                    buttons.push(
                                        {
                                        name: 'del',
                                        icon: 'fa fa-trash',
                                        title: __('Del'),
                                        extend: 'data-toggle="tooltip"',
                                        classname: 'btn btn-xs btn-danger btn-delone'
                                        }
                                        );
                                }
                                // console.log(row.task_status);

                                if(row.task_status=="未发布") {
                                    buttons.push(
                                        {
                                            name: 'btn_1',
                                            text: "发布任务",
                                            extend: 'data-toggle="tooltip" data-name="btn_1" data-title="发布任务" ',
                                            classname: 'btn btn-xs btn-success btn-magic btn-div',
                                        });
                                }

                                $(table).data("operate-edit", null);
                                return Table.api.buttonlink(this, buttons, value, row, index, 'operate');
                            }}
                    ]
                ]
            });


            $(document).on("click", "#sendTask", function (e){
                var data = table.bootstrapTable('getSelections');
                var table_select = JSON.stringify(table.bootstrapTable('getSelections'));
                layer.confirm('确定发布？',{
                    btn: ['确定','取消'] //可以无限个按钮
                    },function(index, layero){
                        Fast.api.ajax({
                            url: 'shop/task/lists/update_status',
                            data: {
                             'ids': data,

                            },
                            success: function (data) {
                                table.bootstrapTable('refresh');
                               layer.msg(data.msg);
                            },
                            error: function (data) {
                                table.bootstrapTable('refresh');
                               //失败的回调
                              layer.msg(data.msg);
                            }
                        });
                    },function(index, layero){
                        layer.msg('已取消操作。');
                    }
                );
            });

            $(document).on("click", "#add", function (e){
                var data = table.bootstrapTable('getSelections');
                var post_data = JSON.stringify(data);
                var data = table.bootstrapTable('getSelections');
                var post_data = JSON.stringify(data);
                var btn_array = ['确定', '保存草稿'];
                var btn_evn_array = {
                    evn_1:function (index,layero) {
                         var frame = Layer.getChildFrame('html', index);
                         $("#type",frame).val('send');

                        var form = $("form",frame);
                        var success = function (data, ret) {
                            var data = data.responseJSON;
                            if(data.code == 1){
                                layer.msg(data.msg);
                                table.bootstrapTable('refresh');
                                Layer.close(index);
                            }else{
                                layer.alert(data.msg)
                            }
                        }
                        Fast.api.form_ajax_submit(form,success);
                         return false;
                    },
                    evn_2:function (index,layero) {
                        var frame = Layer.getChildFrame('html', index);
                        console.log(2);
                        $("#type", frame).val('draft');
                        var form = $("form",frame);
                        var success = function (data, ret) {
                            var data = data.responseJSON;
                            //return;
                            if(data.code == 1){
                                layer.msg(data.msg);
                                table.bootstrapTable('refresh');
                                Layer.close(index);
                            }else{

                                layer.alert(data.msg);
                            }
                        }
                        Fast.api.form_ajax_submit(form,success);
                        return false;
                    }
                };
                //console.log(Fast);
                Fast.api.new_open("shop/task/lists/add?data="+post_data, __('添加任务'),btn_array,btn_evn_array,2);
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(document).on("click","#next_1",function(){
                $("#shop_choose").removeClass('active');
                $("#tab_1").removeClass('active');
                $("#module").addClass('active');
                $("#tab_2").addClass('active');
            });
            $(document).on("click","#last_1",function(){
                $("#shop_choose").addClass('active');
                $("#tab_1").addClass('active');
                $("#module").removeClass('active');
                $("#tab_2").removeClass('active');
            });

            $(document).on("click","#next_2",function(){
                $("#module").removeClass('active');
                $("#tab_2").removeClass('active');
                $("#others").addClass('active');
                $("#tab_3").addClass('active');
            });

            $(document).on("click","#last_2",function(){
                $("#module").addClass('active');
                $("#tab_2").addClass('active');
                $("#others").removeClass('active');
                $("#tab_3").removeClass('active');
            });

            $(document).on("click","#ok",function(){
                $("#type").val('send');
                $("#add-form").submit();
            });


            $(document).on('change', '#c-platform_type', function(){
                var value = $("#c-platform_type").val();
                Fast.api.ajax({
                    url: 'shop/task/lists/getShop',
                    data: {
                     'shop_type': value,
                    },
                    success: function (data) {
                        var str = '<select class="form-control chosen-select selectpicker" name="row[shop_id]" data-rule="required" id="c-shop_id"  data-live-search="true">\
                            <option value="0">请选择店铺</option>';
                        if(data.code == 1){
                            var arr = data.data;
                            var len = arr.length;
                            console.log(len)
                            if(len > 0){
                                for (var i = 0; i < len; i++) {
                                    str += '<option value="'+arr[i].shop_id+'">'+arr[i].shop_name+'</option>';
                                }
                            }
                            str += '</select>';
                            console.log(str);
                            $("#shop").html(str);
                            Controller.api.bindevent();return;
                        }else{
                            layer.msg(data.msg);
                        }

                    },
                    error: function (data) {
                       //失败的回调
                      layer.msg(data.msg);
                    }
                });

            });

            var html = '';
            $(document).on("click","#add_key",function(){
                var len = $("#key_word").children().length;
                //console.log(len)
                if(len > 9){
                    alert('最多只能添加十个关键字');
                    return false;
                }
                len++;
                html = '<div id="key_'+len+'">\
                            <div class="col-xs-12 col-sm-6">\
                                <div class="form-group">\
                                    <label class="control-label col-xs-12 col-sm-4"></label>\
                                    <div class="col-xs-12 col-sm-4">\
                                        <input id="" data-rule="required" class="form-control form-control" name="row[key_word][]" type="text">\
                                    </div>\
                                </div>\
                            </div>\
                            <div class="col-xs-12 col-sm-6">\
                                <div class="form-group">\
                                    <label class="control-label col-xs-12 col-sm-4"></label>\
                                    <div class="col-xs-12 col-sm-4">\
                                        <input id="" data-rule="required" class="form-control form-control" name="row[task_num][]" type="num">\
                                    </div>\
                                </div>\
                            </div>\
                        </div>';
                $("#key_word").append(html)
            });

            $(document).on("click","#del_key",function(){
                var len = $("#key_word").children().length;
                if(len < 2){
                    alert('至少要保留一个关键字');
                    return false;
                }
                $("#key_"+len).remove();
                len--;
            });

            //改为否
            $(document).on("click","#task_type_3",function(){

                $("#is_collect_shop_2").prop("checked",true);
                $("#is_collect_shop_1").prop("checked",false);

                $("#Is_collect_goods_2").prop("checked",true);
                $("#Is_collect_goods_1").prop("checked",false);


                $("#Is_add_goods_2").prop("checked",true);
                $("#Is_add_goods_1").prop("checked",false);


                $("#is_fake_check_2").prop("checked",true);
                $("#is_fake_check_1").prop("checked",false);


                $("#Is_good_evalute_2").prop("checked",true);
                $("#Is_good_evalute_1").prop("checked",false);


                $("#is_review_2").prop("checked",true);
                $("#is_review_1").prop("checked",false);


                $("#is_review_slide_show_2").prop("checked",true);
                $("#is_review_slide_show_1").prop("checked",false);

                $("#is_need_send_2").prop("checked",true);
                $("#is_need_send_1").prop("checked",false);


                $("#c_beat_price").css("display","none");
                $("#c-beat_price").val("");
                $("#c_service_change_price").css("display","none");
                $("#c-service_change_price").val("");

            });
            //追评晒图和追评绑在一起
            $(document).on("click","#is_review_1",function(){
                $("#is_review_slide_show_2").prop("checked",false);
                $("#is_review_slide_show_1").prop("checked",true);
            });
            $(document).on("click","#is_review_2",function(){
                $("#is_review_slide_show_1").prop("checked",false);
                $("#is_review_slide_show_2").prop("checked",true);
            });

            $(document).on("click","#is_review_slide_show_1",function(){
                $("#is_review_2").prop("checked",false);
                $("#is_review_1").prop("checked",true);
            });
            $(document).on("click","#is_review_slide_show_2",function(){
                $("#is_review_1").prop("checked",false);
                $("#is_review_2").prop("checked",true);
            });



            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});